package com.work.yly.website.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.work.yly.website.service.ServiceDefine;
import com.work.yly.website.service.ServiceHelper;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Controller
@RequestMapping("/about")
public class AboutController extends BaseController{
	@RequestMapping("/index")
	public String index() {
		return "/about/index";
	}
	
	@RequestMapping("/faq")
	public String faq() {
		return "/about/faq";
	}
	
	@RequestMapping("/contactus")
	public String contactus() {
		return "/about/contactus";
	}

	@RequestMapping("/introduce")
	public String introduce() {
		return "/about/introduce";
	}

	@RequestMapping("/slicense")
	public String slicense() {
		return "/about/slicense";
	}

	@RequestMapping("/cfq")
	public String cfq(HttpServletRequest request, Model model){
		String pagenum = request.getParameter("pagenum");
		String pagesize = request.getParameter("pagesize");
		if(StringUtils.isEmpty(pagenum)){
			pagenum = "1";
		}
		if(StringUtils.isEmpty(pagesize)){
			pagesize = "5";
		}
		JSONObject obj = ServiceHelper.queryPageZxxx(Integer.valueOf(pagenum), Integer.valueOf(pagesize));
		if (obj.getInt("flag") == ServiceDefine.CALL_SUCCESS) {
			JSONArray arr = obj.getJSONObject("data").getJSONArray("zxxx_list");
			List<Map<String, Object>> list = Lists.newArrayList();
			for(int i=0; i<arr.size(); i++){
				JSONObject o = arr.getJSONObject(i);
				Map<String, Object> map = Maps.newHashMap();
				map.put("fbsj", o.getString("fbsj"));
				map.put("zxbt", com.work.yly.website.service.StringUtils.abbr(o.getString("zxbt"),48));
				map.put("zxfm", o.getString("zxfm"));
				map.put("zxnr", com.work.yly.website.service.StringUtils.abbr(com.work.yly.website.service.StringUtils.replaceHtml(o.getString("zxnr")),180));
				list.add(map);	
			}
			model.addAttribute("totalCount", obj.getJSONObject("data").getInt("totalCount"));
			model.addAttribute("pagenum", obj.getJSONObject("data").getInt("pagenum"));
			model.addAttribute("pagesize", obj.getJSONObject("data").getInt("pagesize"));
			model.addAttribute("list", list);
		}
		return "/about/cfq";
	}
	
	@RequestMapping("/zxxx")
	public String zxxx(HttpServletRequest request, Model model){
		String zxxx_id = request.getParameter("zxxx_id");
		JSONObject obj = ServiceHelper.queryZxxxById(Integer.valueOf(zxxx_id));
		if (obj.getInt("flag") == ServiceDefine.CALL_SUCCESS) {
			model.addAttribute("zxbt", obj.getJSONObject("data").getString("zxbt"));
			model.addAttribute("zxnr", obj.getJSONObject("data").getString("zxnr"));
			model.addAttribute("fbsj", obj.getJSONObject("data").getString("fbsj"));
			model.addAttribute("fbry", obj.getJSONObject("data").getString("fbsj"));
		}
		return "/about/zxxx";
	}
}
